<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Minim : : WindowFunction</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body> 
<center>
<table class="mainTable">
  <tr>
    <td class="header">
    	<span class="indexheader">Minim</span><br/>
    	<span class="indexnavigation">
    		<a href="index.html">core</a> | 
    		<a href="index_ugens.html">ugens</a> | 
    		<a href="index_analysis.html">analysis</a>
    	</span>
    </td>
    <td class="border-left">&nbsp;</td>
  </tr>
  
  <tr>
    <td class="classNavigation">
    	<p class="mainTextName">WindowFunction</p>
    	
    	
       <p class="linkListHeader">Methods</p>
       <p class="linkList">
           
    <a href="windowfunction_method_apply.html" title="Apply the window function to a sample buffer.">apply ( )</a><br/>
    
    <a href="windowfunction_method_generatecurve.html" title="Generates the curve of the window function.">generateCurve ( )</a><br/>
    
	   </p>
   
    </td>
    <td class="mainText border-left">
    	A Window function represents a curve which is applied to a sample buffer to
 reduce the introduction of spectral leakage in the Fourier transform.

 <p>
 <b>Windowing</b>
 <p>
 Windowing is the process of shaping the audio samples before transforming them
 to the frequency domain. The Fourier Transform assumes the sample buffer is is a 
 repetitive signal, if a sample buffer is not truly periodic within the measured
 interval sharp discontinuities may arise that can introduce spectral leakage.
 Spectral leakage is the speading of signal energy across multiple FFT bins. This
 "spreading" can drown out narrow band signals and hinder detection.
 <p>
 A <a href="http://en.wikipedia.org/wiki/Window_function">windowing function</a>
 attempts to reduce spectral leakage by attenuating the measured sample buffer
 at its end points to eliminate discontinuities. If you call the <code>window()</code> 
 function with an appropriate WindowFunction, such as <code>HammingWindow()</code>,
 the sample buffers passed to the object for analysis will be shaped by the current
 window before being transformed. The result of using a window is to reduce
 the leakage in the spectrum somewhat.
 <p>
 <code>WindowFunction</code> handles work associated with various window functions 
 such as the Hamming window. To create your own window function you must extend
 <code>WindowFunction</code> and implement the {@link #value(int, int) value}
 method which defines the shape of the window at a given offset. 
 <code>WindowFunction</code> will call this method to apply the window to 
 a sample buffer. The number passed to the method is an offset within the length
 of the window curve.
    	<p class="memberSectionHeader">Constructors</p>
    	<pre><em></em>
WindowFunction()
</pre>
    	
    	<p class="memberSectionHeader">Related</p>
    	<A href="fft_class_fft.html">FFT</A><BR>

    	<p class="memberSectionHeader">Example</p>
    	<pre>/**
  * This sketch demonstrates how to use an FFT to analyze an 
  * AudioBuffer and draw the resulting spectrum. &lt;br />
  * It also allows you to turn windowing on and off, 
  * but you will see there is not much difference in the spectrum.&lt;br />
  * Use the number keys to choose different windows.
  * &lt;p>
  * For more information about Minim and additional features, 
  * visit http://code.compartmental.net/minim/
  */

import ddf.minim.analysis.*;
import ddf.minim.*;

Minim minim;
AudioPlayer jingle;
FFT fft;
String windowName;

void setup()
{
  size(512, 200, P3D);
  
  minim = new Minim(this);
  
  jingle = minim.loadFile("jingle.mp3", 2048);
  jingle.loop();
  
  // create an FFT object that has a time-domain buffer 
  // the same size as jingle's sample buffer
  // note that this needs to be a power of two 
  // and that it means the size of the spectrum
  // will be 512. see the online tutorial for more info.
  fft = new FFT(jingle.bufferSize(), jingle.sampleRate());
  
  textFont(createFont("Arial", 16));
  
  windowName = "Rectangular Window";
}

void draw()
{
  background(0);
  stroke(255);
  // perform a forward FFT on the samples in jingle's left buffer
  // note that if jingle were a MONO file, 
  // this would be the same as using jingle.right or jingle.left
  fft.forward(jingle.mix);
  for(int i = 0; i &lt; fft.specSize(); i++)
  {
    // convert the magnitude to a DB value. 
    // this means values will range roughly from 0 for the loudest
    // bands to some negative value.
    float bandDB = 20 * log( 2 * fft.getBand(i) / fft.timeSize() );
    // so then we want to map our DB value to the height of the window
    // given some reasonable range
    float bandHeight = map( bandDB, 0, -150, 0, height );
    line(i, height, i, bandHeight );
  }
  fill(255);
  // keep us informed about the window being used
  text("The window being used is: " + windowName, 5, 20);
}

void keyReleased()
{
  WindowFunction newWindow = FFT.NONE;
  
  if ( key == '1' ) 
  {
    newWindow = FFT.BARTLETT;
  }
  else if ( key == '2' )
  {
    newWindow = FFT.BARTLETTHANN;
  }
  else if ( key == '3' )
  {
    newWindow = FFT.BLACKMAN;
  }
  else if ( key == '4' )
  {
    newWindow = FFT.COSINE;
  }
  else if ( key == '5' )
  {
    newWindow = FFT.GAUSS;
  }
  else if ( key == '6' )
  {
    newWindow = FFT.HAMMING;
  }
  else if ( key == '7' )
  {
    newWindow = FFT.HANN;
  }
  else if ( key == '8' )
  {
    newWindow = FFT.LANCZOS;
  }
  else if ( key == '9' )
  {
    newWindow = FFT.TRIANGULAR;
  }

  fft.window( newWindow );
  windowName = newWindow.toString();
}
</pre>
    	<p class="memberSectionHeader">Usage</p>
    	Web & Application
    </td>
  </tr>
</table>
</center>
</body>
</html>